package com.simon.annual.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import com.simon.annual.entity.ReportAtxnOccasional;
import com.simon.annual.entity.ReportAtxnRoutine;
import com.simon.annual.entity.ReportExpropriation;
import com.simon.annual.entity.ReportGuarantee;
import com.simon.annual.entity.ReportLawsuit;
import com.simon.annual.entity.ReportNeeq;
import com.simon.annual.entity.ReportRestrictedasset;
import com.simon.annual.repositoy.IReportAtxnOccasionalRepositoy;
import com.simon.annual.repositoy.IReportAtxnRoutineRepositoy;
import com.simon.annual.repositoy.IReportExpropriationRepositoy;
import com.simon.annual.repositoy.IReportGuaranteeRepositoy;
import com.simon.annual.repositoy.IReportLawsuitRepositoy;
import com.simon.annual.repositoy.IReportRestrictedassetRepositoy;
import com.simon.annual.service.ICompanyPublicInfo;

/**
 * 第五节:重要事项
 * */
@Service
public class MajorIssuesImpl{

	@Resource
	private ICompanyPublicInfo cmpanyPublicInfo;
	@Resource
	private IReportRestrictedassetRepositoy reportRestrictedassetRepositoy;
	@Resource
	private IReportAtxnRoutineRepositoy reportAtxnRoutineRepositoy;
	@Resource
	private IReportLawsuitRepositoy reportLawsuitRepositoy;
	@Resource
	private IReportGuaranteeRepositoy reportGuaranteeRepositoy;
	@Resource
	private IReportExpropriationRepositoy reportExpropriationRepositoy;
	@Resource
	private IReportAtxnOccasionalRepositoy reportAtxnOccasionalRepositoy;
	
	public ReportNeeq analysis(Document doc, ReportNeeq reportNeeq, int companyPtrId) {
		
		Element  els = doc.getElementsMatchingOwnText("\u7B2C\u4E94\u8282").get(1);
		//第一节：重要事项索引
		Element major = cmpanyPublicInfo.findNextTag("table",els);
		Elements majorTd = major.select("table tr > *");
		//是否存在重大诉讼、仲裁事项
		reportNeeq.setHasMajorLawsuit(majorTd.get(4).text());
		reportNeeq.setMajorLawsuitIndex(majorTd.get(5).text());
		if(majorTd.get(5).text().length()>1){
			Elements ins = doc.getElementsMatchingOwnText("\u91CD\u5927\u8BC9\u8BBC");
			Element majorContent = null;
			for (Element element : ins) {
				if(els.parentNode().siblingIndex()<=element.parentNode().siblingIndex()&&element.parent().tagName().equals("body")){
					majorContent = cmpanyPublicInfo.findNextTag("table", element);
					break;
				}
			}
			Elements majorContents = majorContent.select("table tr");
			List<ReportLawsuit> reportLawsuitList = new ArrayList<ReportLawsuit>();
			for (int i = 0; i < majorContents.size(); i++) {
				if(i!=0){
					Elements tds = majorContents.get(i).children();
					ReportLawsuit reportLawsuit = new ReportLawsuit();
					reportLawsuit.setName(tds.get(0).text());
					reportLawsuit.setValueInvolved(tds.get(1).text());
					reportLawsuit.setPct_NA(tds.get(2).text());
					reportLawsuit.setIsFinished(tds.get(3).text());
					reportLawsuit.setDisclosureDate(tds.get(4).text());
					reportLawsuit.setProjectScopeId(companyPtrId);
					reportLawsuitList.add(reportLawsuit);
				}
			}
			reportLawsuitRepositoy.save(reportLawsuitList);
			reportNeeq.setLawsuitReview(cmpanyPublicInfo.findNextTag("table",majorContent).text());
		}
		//是否存在对外担保事项
		reportNeeq.setHasExternalGuarantee(majorTd.get(7).text());
		reportNeeq.setExternalGuaranteeIndex(majorTd.get(8).text());
		if(majorTd.get(8).text().length()>1){
			Elements ins = doc.getElementsMatchingOwnText("\u5BF9\u5916\u62C5\u4FDD");
			Element majorContent = null;
			for (Element element : ins) {
				if(els.parentNode().siblingIndex()<=element.parentNode().siblingIndex()&&element.parent().tagName().equals("body")){
					majorContent = cmpanyPublicInfo.findNextTag("table", element);
					break;
				}
			}
//			Element majorContent =cmpanyPublicInfo.findNextTag("table", major, index);
			Elements majorContents = majorContent.select("table tr");
			List<ReportGuarantee> reportGuaranteeList = new ArrayList<ReportGuarantee>();
			for (int i = 0; i < majorContents.size()-1; i++) {
				if(i!=0){
					Elements tds = majorContents.get(i).children();
					ReportGuarantee reportGuarantee = new ReportGuarantee();
					reportGuarantee.setObject(tds.get(0).text());
					reportGuarantee.setValue(tds.get(1).text());
					reportGuarantee.setType(tds.get(2).text());
					reportGuarantee.setResponsibility(tds.get(3).text());
					reportGuarantee.setIsProcedureLegitimate(tds.get(4).text());
					reportGuarantee.setIsAffiliated(tds.get(5).text());
					reportGuarantee.setProjectScopeId(companyPtrId);
					reportGuaranteeList.add(reportGuarantee);
				}
			}
			reportGuaranteeRepositoy.save(reportGuaranteeList);
			//对外担保分类汇总
			Element guaranty = cmpanyPublicInfo.findNextTag("table",majorContent);
			Elements tds = guaranty.select("table tr > *");
			reportNeeq.setExternalGuarantyValue(tds.get(3).text());
			reportNeeq.setGuarantyForShareholderOrControllerValue(tds.get(5).text());
			reportNeeq.setGuranty_for_high_ALR_value(tds.get(7).text());
			reportNeeq.setGuaranty_over_half_NA(tds.get(9).text());
			//清偿情况
			Element payOff = cmpanyPublicInfo.findNextTag("table",guaranty);
			reportNeeq.setGuaranteePayOffReview(payOff.text());
		}
		//是否存在控股股东、实际控制人及其关联方占用或转移公司资金、资产的情况
		reportNeeq.setHasExpropriation(majorTd.get(10).text());
		reportNeeq.setExpropriationIndex(majorTd.get(11).text());
		if(majorTd.get(11).text().length()>1){
			Elements ins = doc.getElementsMatchingOwnText("\u5360\u7528\u6216\u8F6C\u79FB\u516C\u53F8\u8D44\u91D1");
			Element majorContent = null;
			for (Element element : ins) {
				if(els.parentNode().siblingIndex()<=element.parentNode().siblingIndex()&&element.parent().tagName().equals("body")){
					majorContent = cmpanyPublicInfo.findNextTag("table", element);
					break;
				}
			}
			Elements majorContents = majorContent.select("table tr");
			List<ReportExpropriation> reportExpropriationList = new ArrayList<ReportExpropriation>();
			for (int i = 0; i < majorContents.size()-1; i++) {
				if(i!=0){
					Elements tds = majorContents.get(i).children();
					ReportExpropriation reportExpropriation = new ReportExpropriation();
					reportExpropriation.setOccupant(tds.get(0).text());
					reportExpropriation.setAsset(tds.get(1).text());
					reportExpropriation.setBgnBalance(tds.get(2).text());
					reportExpropriation.setAccumAmount(tds.get(3).text());
					reportExpropriation.setEndBalance(tds.get(4).text());
					reportExpropriation.setEndBalance(tds.get(5).text());
					reportExpropriation.setProjectScopeId(companyPtrId);
					reportExpropriationList.add(reportExpropriation);
				}
			}
			reportExpropriationRepositoy.save(reportExpropriationList);
			//占用原因、归还及整改情况：
			reportNeeq.setExpropriationReview(cmpanyPublicInfo.findNextTag("table", majorContent).text());
		}
		//是否存在日常性关联交易事项
		reportNeeq.setHasRoutineAffiliateTxn(majorTd.get(13).text());
		reportNeeq.setRoutineAffiliateTxnIndex(majorTd.get(14).text());
		if(majorTd.get(14).text().length()>1){			
			Elements ins = doc.getElementsMatchingOwnText("\u5076\u53D1\u6027\u5173\u8054\u4EA4\u6613\u60C5\u51B5");
			Element majorContent = null;
			for (Element element : ins) {
				if(els.parentNode().siblingIndex()<=element.parentNode().siblingIndex()&&element.parent().tagName().equals("body")){
					majorContent = cmpanyPublicInfo.findNextTag("table", element);
					break;
				}
			}
			Elements majorContents = majorContent.select("table tr");
			List<ReportAtxnRoutine> reportAtxnRoutineList = new ArrayList<ReportAtxnRoutine>();
			List<ReportAtxnOccasional> reportAtxnOccasionalList = new ArrayList<ReportAtxnOccasional>();
			boolean flag =true;
			for (int i = 2; i < majorContents.size()-2; i++) {
				if(flag){
					Elements tds = majorContents.get(i).children();
					if(tds.size()!=4){
						flag = false;
						break;
					}
					ReportAtxnRoutine reportAtxnRoutine = new ReportAtxnRoutine();
					reportAtxnRoutine.setName(tds.get(0).text());
					reportAtxnRoutine.setPredictedAmount(tds.get(1).text());
					reportAtxnRoutine.setActualAmount(tds.get(2).text());
					reportAtxnRoutine.setProjectScopeId(companyPtrId);
					reportAtxnRoutineList.add(reportAtxnRoutine);
				}else{
					//偶发性关联交易的必要性、持续性以及对公司生产经营的影响
					Elements tds = majorContents.get(i).nextElementSibling().children();
					ReportAtxnOccasional reportAtxnOccasional = new ReportAtxnOccasional();
					reportAtxnOccasional.setAffParty(tds.get(0).text());
					reportAtxnOccasional.setContent(tds.get(1).text());
					reportAtxnOccasional.setTxnAmount(tds.get(2).text());
					reportAtxnOccasional.setIsProcedureLegitimate(tds.get(3).text());
					reportAtxnOccasional.setProjectScopeId(companyPtrId);
					reportAtxnOccasionalList.add(reportAtxnOccasional);
				}
			}
			reportAtxnRoutineRepositoy.save(reportAtxnRoutineList);
			reportAtxnOccasionalRepositoy.save(reportAtxnOccasionalList);
		}
		//偶发性关联交易的必要性、持续性以及对公司生产经营的影响
		/*reportNeeq.setHasAffiliateTxnOccas(majorTd.get(16).text());
		reportNeeq.setAffiliateTxnOccasIndex(majorTd.get(17).text());
		if(majorTd.get(16).text().equals("是")){
			index++;
			Element majorContent =findNextTag("table", major, index);
			Elements majorContents = majorContent.select("table tr");
			List<ReportAtxnOccasional> reportAtxnOccasionalList = new ArrayList<ReportAtxnOccasional>();
			for (int i = 0; i < majorContents.size()-1; i++) {
				if(i>1){
					Elements tds = majorContents.get(i).select("td");
					ReportAtxnOccasional reportAtxnOccasional = new ReportAtxnOccasional();
					reportAtxnOccasional.setAffParty(tds.get(0).text());
					reportAtxnOccasional.setContent(tds.get(1).text());
					reportAtxnOccasional.setTxnAmount(tds.get(2).text());
					reportAtxnOccasional.setIsProcedureLegitimate(tds.get(3).text());
					reportAtxnOccasional.setProjectScopeId(companyPtrId);
					reportAtxnOccasionalList.add(reportAtxnOccasional);
				}
			}
//			reportAtxnRoutineRepositoy.save(reportLawsuitList);
			reportNeeq.setAffiliateTxnOccasReview(findNextTag("table", majorContent).text());
			index++;
		}*/
		//是否存在经股东大会审议过的收购、出售资产事项
		reportNeeq.setHasDeliberatedAssetReorganizations(majorTd.get(16).text());
		reportNeeq.setDeliberatedAssetReorganizationsIndex(majorTd.get(17).text());
		if(majorTd.get(17).text().length()>1){
			Elements ins = doc.getElementsMatchingOwnText("\u6536\u8D2D\u3001\u51FA\u552E");
			Element majorContent = null;
			for (Element element : ins) {
				if(els.parentNode().siblingIndex()<=element.parentNode().siblingIndex()&&element.parent().tagName().equals("body")){
					majorContent = cmpanyPublicInfo.findNextTag("table", element);
					break;
				}
			}
			reportNeeq.setDeliberatedAssetReorganizationsReview(majorContent.text());
		}
		//是否存在经股东大会审议过的对外投资事项
		reportNeeq.setHasDeliberatedInvestement(majorTd.get(19).text());
		reportNeeq.setDeliberatedInvestementIndex(majorTd.get(20).text());
		if(majorTd.get(20).text().length()>1){
			Elements ins = doc.getElementsMatchingOwnText("\u5BF9\u5916\u6295\u8D44\u4E8B\u9879");
			Element majorContent = null;
			for (Element element : ins) {
				if(els.parentNode().siblingIndex()<=element.parentNode().siblingIndex()&&element.parent().tagName().equals("body")){
					majorContent = cmpanyPublicInfo.findNextTag("table", element);
					break;
				}
			}
			reportNeeq.setDeliberatedInvestementReview(majorContent.text());
		}
		//是否存在经股东大会审议过的企业合并事项
		reportNeeq.setHasDeliberatedConsolidation(majorTd.get(22).text());
		reportNeeq.setDeliberatedConsolidationIndex(majorTd.get(23).text());
		if(majorTd.get(23).text().length()>1){
			Elements ins = doc.getElementsMatchingOwnText("\u4F01\u4E1A\u5408\u5E76\u4E8B\u9879");
			Element majorContent = null;
			for (Element element : ins) {
				if(els.parentNode().siblingIndex()<=element.parentNode().siblingIndex()&&element.parent().tagName().equals("body")){
					majorContent = cmpanyPublicInfo.findNextTag("table", element);
					break;
				}
			}
			reportNeeq.setDeliberatedConsolidationReview(majorContent.text());
		}
		//是否存在股权激励事项
		reportNeeq.setHasEquityIncentive(majorTd.get(25).text());
		reportNeeq.setEquityIncentiveIndex(majorTd.get(26).text());
		if(majorTd.get(26).text().length()>1){
			Elements ins = doc.getElementsMatchingOwnText("\u80A1\u6743\u6FC0\u52B1\u8BA1\u5212");
			Element majorContent = null;
			for (Element element : ins) {
				if(els.parentNode().siblingIndex()<=element.parentNode().siblingIndex()&&element.parent().tagName().equals("body")){
					majorContent = cmpanyPublicInfo.findNextTag("table", element);
					break;
				}
			}
			reportNeeq.setEquityIncentiveReview(majorContent.text());
		}
		//是否存在已披露的承诺事项
		reportNeeq.setHasCommitment(majorTd.get(28).text());
		reportNeeq.setCommitmentIndex(majorTd.get(29).text());
		if(majorTd.get(29).text().length()>1){
			Elements ins = doc.getElementsMatchingOwnText("\u627F\u8BFA\u4E8B\u9879");
			Element majorContent = null;
			for (Element element : ins) {
				if(els.parentNode().siblingIndex()<=element.parentNode().siblingIndex()&&element.parent().tagName().equals("body")){
					majorContent = cmpanyPublicInfo.findNextTag("table", element);
					break;
				}
			}
			reportNeeq.setCommitmentReview(majorContent.html());
		}
		//是否存在资产被查封、扣押、冻结或者被抵押、质押的情况
		reportNeeq.setHasRestrictedAsset(majorTd.get(31).text());
		reportNeeq.setRestrictedAssetIndex(majorTd.get(32).text());
		if(majorTd.get(32).text().length()>1){
			Elements ins = doc.getElementsMatchingOwnText("\u51BB\u7ED3\u6216\u8005\u88AB\u62B5\u62BC");
			Element majorContent = null;
			for (Element element : ins) {
				if(els.parentNode().siblingIndex()<=element.parentNode().siblingIndex()&&element.parent().tagName().equals("body")){
					majorContent = cmpanyPublicInfo.findNextTag("table", element);
					break;
				}
			}
			Elements majorContents = majorContent.select("table tr");
			List<ReportRestrictedasset> reportRestrictedassetList = new ArrayList<ReportRestrictedasset>();
			for (int i = 0; i < majorContents.size()-1; i++) {
				if(i!=0){
					Elements tds = majorContents.get(i).children();
					ReportRestrictedasset reportRestrictedasset = new ReportRestrictedasset();
					reportRestrictedasset.setAsset(tds.get(0).text());
					reportRestrictedasset.setRestrictionType(tds.get(1).text());
					reportRestrictedasset.setBookValue(tds.get(2).text());
					reportRestrictedasset.setReason(tds.get(4).text());
					reportRestrictedasset.setProjectScopeId(companyPtrId);
					reportRestrictedassetList.add(reportRestrictedasset);
				}
			}
			reportRestrictedassetRepositoy.save(reportRestrictedassetList);
		}
		//是否存在被调查处罚的事项
		reportNeeq.setHasPenalty(majorTd.get(34).text());
		reportNeeq.setPenaltyIndex(majorTd.get(35).text());
		if(majorTd.get(35).text().length()>1){
			Elements ins = doc.getElementsMatchingOwnText("\u8C03\u67E5\u5904\u7F5A");
			Element majorContent = null;
			for (Element element : ins) {
				if(els.parentNode().siblingIndex()<=element.parentNode().siblingIndex()&&element.parent().tagName().equals("body")){
					majorContent = cmpanyPublicInfo.findNextTag("table", element);
					break;
				}
			}
			reportNeeq.setPenaltyReview(majorContent.text());
		}
		//是否存在重大资产重组的事项
		reportNeeq.setHasAssetsReorganization(majorTd.get(37).text());
		reportNeeq.setAssetsReorganizationIndex(majorTd.get(38).text());
		if(majorTd.get(38).text().length()>1){
			Elements ins = doc.getElementsMatchingOwnText("\u91CD\u5927\u8D44\u4EA7\u91CD\u7EC4");
			Element majorContent = null;
			for (Element element : ins) {
				if(els.parentNode().siblingIndex()<=element.parentNode().siblingIndex()&&element.parent().tagName().equals("body")){
					majorContent = cmpanyPublicInfo.findNextTag("table", element);
					break;
				}
			}
			reportNeeq.setAssetsReorganizationReview(majorContent.text());
		}
		//是否存在媒体普遍质疑的事项
		reportNeeq.setHasMediaQuestioned(majorTd.get(40).text());
		reportNeeq.setMediaQuestionedIndex(majorTd.get(41).text());
		if(majorTd.get(41).text().length()>1){
			Elements ins = doc.getElementsMatchingOwnText("\u5A92\u4F53\u666E\u904D\u8D28\u7591");
			Element majorContent = null;
			for (Element element : ins) {
				if(els.parentNode().siblingIndex()<=element.parentNode().siblingIndex()&&element.parent().tagName().equals("body")){
					majorContent = cmpanyPublicInfo.findNextTag("table", element);
					break;
				}
			}
			reportNeeq.setMediaQuestionedReview(majorContent.text());
		}
		//是否存在自愿披露的重要事项
		reportNeeq.setHasVoluntaryDisclosureKeyIssue(majorTd.get(43).text());
		reportNeeq.setVoluntaryDisclosureKeyIssueIndex(majorTd.get(44).text());
		if(majorTd.get(44).text().length()>1){
			Elements ins = doc.getElementsMatchingOwnText("\u81EA\u613F\u62AB\u9732");
			Element majorContent = null;
			for (Element element : ins) {
				if(els.parentNode().siblingIndex()<=element.parentNode().siblingIndex()&&element.parent().tagName().equals("body")){
					majorContent = cmpanyPublicInfo.findNextTag("table", element);
					break;
				}
			}
			reportNeeq.setPenaltyReview(majorContent.text());
		}
		return reportNeeq;
	}
}
